package converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import java.util.List;

import daos.DAOEditora;
import entidades.Editora;

@FacesConverter(value="converterEditora", forClass=Editora.class)

public class ConverterEditora implements Converter{
		public List<Editora> editoras;
		DAOEditora daoEditora = new DAOEditora();

		public ConverterEditora(){
			editoras = daoEditora.listar();
		}
		 
		public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {  
			if (submittedValue.trim().equals("")) {  
				return null;
			} 
			else{ 
				for (Editora editora : editoras) {  
					if (editora.getEdiNome().equals(submittedValue)) {
						return editora;
					}
				}
			}
			return null;
		}

		public String getAsString(FacesContext facesContext, UIComponent component, Object value) {  
			if (value == null || value.equals("")) {  
				
				return "";  
			} 
			else {  
				return String.valueOf(((Editora) value).getEdiNome());  
			}  
		}
	}